﻿using System;
using System.Collections.Generic;
using INSE6260.OnlineBanking.Infrastructure;
using INSE6260.OnlineBanking.Model.Accounts;
using INSE6260.OnlineBanking.Model.Payee;
using INSE6260.OnlineBanking.Model.Transaction;
using System.Linq;

namespace INSE6260.OnlineBanking.Repository.EF.Repositories
{
    public class TransactionRepository : RepositoryBase<Transaction>,ITransactionRepository
    {
        public TransactionRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
            
        }
        public List<PaymentHistory> GetPaymentHistory(int clientId,DateTime? fromDate,DateTime? toDate)
        {
            var res = from transaction in SourceQuery.OfType<MakePayment>()
                      join fromAccount in GetDbSet<Account>() on transaction.FromAccountId equals fromAccount.Id
                      join account in GetDbSet<Account>() on transaction.ToAccountId equals account.Id
                      join payeeName in GetDbSet<PayeeName>() on account.Id equals payeeName.AccountId
                      join payee in GetDbSet<Payee>() on payeeName.Id equals payee.PayeeNameId
                      where fromAccount.ClientId == clientId && transaction.TransactionDate >= fromDate &&
                          transaction.TransactionDate <= toDate
                     
                      select new PaymentHistory
                              {
                                  PayDate = transaction.TransactionDate,
                                  PayeeName = payeeName.Name + ", "+payee.AccountNo,
                                  Amount = transaction.Amount,
                                  RefNo = transaction.Id
                              };
            return res.OrderByDescending(tr=>tr.PayDate).ToList();
        }

        public List<TransactioHistory> GetTransactionHistory(int accountId, DateTime? fromDate, DateTime? toDate)
        {
            var res = from transaction in SourceQuery
                      join fromAccount in GetDbSet<Account>() on transaction.FromAccountId equals fromAccount.Id
                      join toAccount in GetDbSet<Account>() on transaction.ToAccountId equals toAccount.Id
                      where (fromAccount.Id == accountId ) && transaction.TransactionDate >= fromDate &&
                          transaction.TransactionDate <= toDate

                      select new TransactioHistory
                      {
                          TransactionDate = transaction.TransactionDate,
                          Description = transaction.Description,
                          Amount = transaction.Amount,
                          WithdrawalAmount = transaction.WithdrawalAmount,
                          DepositAmount = transaction.DepositAmount,
                          RefNo = transaction.Id,
                          Balance = transaction.Balance
                      };
            return res.OrderByDescending(tr => tr.TransactionDate).ToList();
        }
    }
}
